package custom;

public class CustomFractionalNumber {
    private long numerator;
    private long denominator;

    public CustomFractionalNumber() {
        denominator = 1;
    }

    public CustomFractionalNumber(long numerator, long denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    public long getNumerator() {
        return numerator;
    }

    public void setNumerator(long numerator) {
        this.numerator = numerator;
    }

    public long getDenominator() {
        return denominator;
    }

    public void setDenominator(long denominator) {
        this.denominator = denominator;
    }

    @Override
    public String toString() {
        return numerator + "/" + denominator;
    }

    public static CustomFractionalNumber addition(CustomFractionalNumber fraction1, CustomFractionalNumber fraction2) {
        long numerator = 0, denominator = 1, temp = 1;
        if (fraction1.getDenominator() == 0 || fraction2.getDenominator() == 0)
            throw new ArithmeticException();
        else if (fraction1.getNumerator() == 0 && fraction2.getNumerator() != 0) {
            numerator = fraction2.getNumerator();
            denominator = fraction2.getDenominator();
        } else if (fraction1.getNumerator() != 0 && fraction2.getNumerator() == 0) {
            numerator = fraction1.getNumerator();
            denominator = fraction1.getDenominator();
        } else if (fraction1.getNumerator() != 0 && fraction2.getNumerator() != 0) {
            numerator = fraction1.getNumerator() * fraction2.getDenominator() + fraction1.getDenominator() * fraction1.getNumerator();
            denominator = fraction1.getDenominator() * fraction2.getDenominator();
            temp = CustomNumber.greatestCommonDivisor(numerator, denominator);
        }
        return new CustomFractionalNumber(numerator / temp, denominator / temp);
    }

    public static void additionSelf(CustomFractionalNumber fraction1, CustomFractionalNumber fraction2) {
        if (fraction1.getDenominator() == 0 || fraction2.getDenominator() == 0)
            throw new ArithmeticException();
        else if (fraction1.getNumerator() == 0 && fraction2.getNumerator() != 0) {
            fraction1.setNumerator(fraction2.getNumerator());
            fraction1.setDenominator(fraction2.getDenominator());
        } else if (fraction2.getNumerator() != 0 && fraction2.getNumerator() != 0) {
            long numerator = fraction1.getNumerator() * fraction2.getDenominator() + fraction1.getDenominator() * fraction2.getNumerator();
            long denominator = fraction1.getDenominator() * fraction2.getDenominator();
            long temp = CustomNumber.greatestCommonDivisor(numerator, denominator);
            fraction1.setNumerator(numerator / temp);
            fraction1.setDenominator(denominator / temp);
        }
    }
}
